package com.mifengs.commonbase.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.mifengs.commonbase.base.BaseApplication;

import static com.mifengs.commonbase.util.PreferenceUtils.LocalUser.IS_RIDER_STATE;


public class PreferenceUtils {
    public static final String SHARED_ACTIVITY_NAME = "mfsc";
    public static final String APP_ISFIRST = "is_first";
    public static final String APP_PHOTO_PATH = "photo_path";
    public static final String APP_PHOTO_END_PATH = "photo_end_path";
    public static final String APP_SEARCH_URL = "search_url";
    public static final String COOKIE = "cookie";
    public static final String ERROR = "error";  //登录的错误code
    public static final String MOBILE_TYPE = "mobile_type";//手机类型
    public static final String LONGITUDE = "longitude";//获取纬度;
    public static final String LATITUDE = "latitude";////获取经度


    public static String getLongitude(Context context) {
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getString(LONGITUDE, "");
    }

    public static void setLongitude(Context context, String longitude) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(LONGITUDE, longitude);
        edit.commit();
    }

    public static String getLatitude(Context context) {
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getString(LATITUDE, "");
    }

    public static void setLatitude(Context context, String latitude) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(LATITUDE, latitude);
        edit.commit();
    }


    public static void saveIsFirst(Context context, boolean isFirst) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putBoolean(APP_ISFIRST, isFirst);
        edit.commit();
    }

    public static boolean getIsFirst(Context context) {
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getBoolean(APP_ISFIRST, true);
    }

    public static void savePhotoPath(Context context, String path) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(APP_PHOTO_PATH, path);
        edit.commit();
    }

    public static void savePhotoEndPath(Context context, String path) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(APP_PHOTO_END_PATH, path);
        edit.commit();
    }

    public static String getPhotoEndPath(Context context) {
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getString(APP_PHOTO_END_PATH, "");
    }
    public static void saveRiderState(Context context, String path) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(IS_RIDER_STATE, path);
        edit.commit();
    }

    public static String getRiderState(Context context) {
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getString(IS_RIDER_STATE, "");
    }


    public static String getPhotoPath(Context context) {
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getString(APP_PHOTO_PATH, "");
    }

    public static void saveSearchUrl(Context context, String path) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(APP_SEARCH_URL, path);
        edit.commit();
    }

    public static String getSearchUrl(Context context) {
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getString(APP_SEARCH_URL, "");
    }


    /**
     * @param context
     * @param cookies
     */
    public static void saveCookies(Context context, String cookies) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(COOKIE, cookies);
        edit.commit();
    }


    public static String getCookies(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preferences.getString(COOKIE, "");
    }


    public static void savePushToken(String token) {
        SharedPreferences.Editor editor = getLocalUser(BaseApplication.getInstance()).edit();
        editor.putString(LocalUser.PUSH_TOKEN, token);
        editor.commit();
    }

    public static String getPushToken(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.PUSH_TOKEN, "");
    }


    /**
     * 保存Token
     *
     * @param token
     */
    public static void saveToken(String token) {
        SharedPreferences.Editor editor = getLocalUser(BaseApplication.getInstance()).edit();
        editor.putString(LocalUser.KEY_TOKEN, token);
        editor.putBoolean(LocalUser.KEY_LOGIN_STAUTS, true);
        editor.commit();
    }


    /**
//     * 保存Contacts
//     *
//     * @param
//     */
//    public static void saveContacts(String type) {
//        SharedPreferences.Editor editor = getLocalUser(MallApplication.getInstance()).edit();
//        editor.putString(LocalUser.CONTACTS, type);
//        editor.commit();
//    }
//
//    /**
//     * 获取Contacts
//     *
//     * @param
//     */
//    public static String getContacts(Context context) {
//        SharedPreferences preferences = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
//        return preferences.getString(LocalUser.CONTACTS, "");
//    }

    /**
     * @param context
     * @param userName
     */
    public static void saveContacts(Context context, String userName) {
        SharedPreferences.Editor edit = context.getSharedPreferences(LocalUser.CONTACTS, Context.MODE_PRIVATE).edit();
        edit.putString(LocalUser.CONTACTS, userName);
        edit.commit();
    }


    public static String getContacts(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.CONTACTS, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.CONTACTS, "");
    }


    /**
     * 退出
     */
    public static void clearLoginInfo() {
        SharedPreferences.Editor editor = getLocalUser(BaseApplication.getInstance()).edit();
        editor.putString(LocalUser.KEY_TOKEN, "");
        editor.putBoolean(LocalUser.KEY_LOGIN_STAUTS, false);
        editor.putString(LocalUser.HX_USER_NAME, "");
        editor.putString(LocalUser.HX_USER_PWD, "");
        editor.putString(LocalUser.USER_MOBILE, "");
        editor.putInt(LocalUser.IS_STORE_USER,0);
        editor.putInt(LocalUser.IS_RIDER_USER,0);
        editor.putString(LocalUser.IS_RIDER_STATE, "");
        editor.putString(LocalUser.KEY_UID, "");
        editor.commit();
    }


    /**
     * 获取Token
     *
     * @return
     */
    public static String getToken() {
        SharedPreferences preferences = PreferenceUtils.getLocalUser(BaseApplication.getInstance());
        return preferences.getString(LocalUser.KEY_TOKEN, "");
    }

    /**
     * 获取uId
     *
     * @return
     */
    public static String getUid(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.KEY_UID, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.KEY_UID, "");
    }

    /**
     * 保存uId
     *
     */
    public static void saveUid(String uId ,Context context) {
        SharedPreferences.Editor editor = context.getSharedPreferences(LocalUser.KEY_UID, Context.MODE_PRIVATE).edit();
        editor.putString(LocalUser.KEY_UID, uId);
        editor.commit();
    }

    /**
     * 获取EosAccount
     *
     * @return
     */
    public static String getEosAccount(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.KEY_EOS_ACCOUNT, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.KEY_EOS_ACCOUNT, "");
    }

    /**
     * 保存EosAccount
     *
     */
    public static void saveEosAccount(String accountName ,Context context) {
        SharedPreferences.Editor editor = context.getSharedPreferences(LocalUser.KEY_EOS_ACCOUNT, Context.MODE_PRIVATE).edit();
        editor.putString(LocalUser.KEY_EOS_ACCOUNT, accountName);
        editor.commit();
    }



    /**
     * 获取登录状态
     *
     * @return
     */
    public static boolean getLoginStatus() {
        SharedPreferences preferences = PreferenceUtils.getLocalUser(BaseApplication.getInstance());
        return preferences.getBoolean(LocalUser.KEY_LOGIN_STAUTS, false);
    }


    public static SharedPreferences getLocalUser(Context context) {
        return context.getSharedPreferences(LocalUser.FILE_NAME, Context.MODE_PRIVATE);
    }

    /**
     * @param context
     * @param userName
     */
    public static void saveHxUserName(Context context, String userName) {
        SharedPreferences.Editor edit = context.getSharedPreferences(LocalUser.HX_USER_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(LocalUser.HX_USER_NAME, userName);
        edit.commit();
    }


    public static String getHxUserName(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.HX_USER_NAME, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.HX_USER_NAME, "");
    }

    /**
     * @param context
     * @param userName
     */
    public static void saveUserName(Context context, String userName) {
        SharedPreferences.Editor edit = context.getSharedPreferences(LocalUser.USER_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(LocalUser.USER_NAME, userName);
        edit.commit();
    }


    public static String getUserName(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.USER_NAME, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.USER_NAME, "");
    }

    /**
     * @param context
     * @param userPwd
     */
    public static void saveHxUserPwd(Context context, String userPwd) {
        SharedPreferences.Editor edit = context.getSharedPreferences(LocalUser.HX_USER_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(LocalUser.HX_USER_PWD, userPwd);
        edit.commit();
    }

    public static String getHxUserPwd(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.HX_USER_NAME, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.HX_USER_PWD, "");
    }

    /**
     * @param context
     * @param phone
     */
    public static void saveUserMobile(Context context, String phone) {
        SharedPreferences.Editor edit = context.getSharedPreferences(LocalUser.USER_MOBILE, Context.MODE_PRIVATE).edit();
        edit.putString(LocalUser.USER_MOBILE, phone);
        edit.commit();
    }

    public static String getUserMobile(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.USER_MOBILE, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.USER_MOBILE, "");
    }

    /**
     * @param context
     * @param phone
     */
    public static void savePhotoUrl(Context context, String phone) {
        SharedPreferences.Editor edit = context.getSharedPreferences(LocalUser.PHOTO_URL, Context.MODE_PRIVATE).edit();
        edit.putString(LocalUser.PHOTO_URL, phone);
        edit.commit();
    }

    public static String getPhotoUrl(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.PHOTO_URL, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.PHOTO_URL, "");
    }

    /**
     * @param context
     * @param orderId
     */
    public static void saveOrderId(Context context, String orderId) {
        SharedPreferences.Editor edit = context.getSharedPreferences(LocalUser.ORDER_ID, Context.MODE_PRIVATE).edit();
        edit.putString(LocalUser.ORDER_ID, orderId);
        edit.commit();
    }

    public static String getOrderId(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.ORDER_ID, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.ORDER_ID, "");
    }

    public static void saveSplashImg(Context context,String url){
        SharedPreferences.Editor edit = context.getSharedPreferences(LocalUser.SPLASH_IMG, Context.MODE_PRIVATE).edit();
        edit.putString(LocalUser.SPLASH_IMG, url);
        edit.commit();
    }

    public static String getSplashImg(Context context){
        SharedPreferences preferences = context.getSharedPreferences(LocalUser.SPLASH_IMG, Context.MODE_PRIVATE);
        return preferences.getString(LocalUser.SPLASH_IMG, "");
    }


    /**
     * 专门保存用户信息,模板
     * 存信息的时候 SharedPreferences.Editor editor = PreferenceUtils.getLocalUser(context).edit();
     * editor.putString(LocalUser.KEY_USERNAME, username);
     * 取信息的时候 SharedPreferences preferences = PreferenceUtils.getLocalUser(context);
     * preferences.getString(LocalUser.KEY_USERNAME, null)
     */
    public static class LocalUser {
        public static final String FILE_NAME = "LOCAL_USER";
        public static final String KEY_LOGIN_STAUTS = "KEY_LOGIN_STAUTS";  // 登录状态
        public static final String KEY_TOKEN = "KEY_TOKEN";  // token
        public static final String USER_ID = "USER_ID";
        public static final String USER_NAME = "USER_NAME";
        public static final String USER_SEX = "USER_SEX";
        public static final String USER_MOBILE = "USER_MOBILE";
        public static final String USER_ICONIMG = "USER_ICONIMG";
        public static final String HX_USER_NAME = "HX_USER_NAME";
        public static final String HX_USER_PWD = "HX_USER_PWD";
        public static final String PHOTO_URL = "PHOTO_URL";
        public static final String ORDER_ID = "ORDER_ID";
        public static final String PUSH_TOKEN = "push_token";
        public static final String PRODUCT_ID = "product_id";
        public static final String CONTACTS = "contacts_id";
        public static final String SPLASH_IMG = "splash_img";
        public static final String SPLASH_STATUS = "splash_status";
        public static final String SPLASH_TYPE = "splash_type";
        public static final String SPLASH_TIME = "splash_time";
        public static final String SPLASH_ROUTE = "splash_route";
        public static final String IS_STORE_USER = "is_store_user";
        public static final String IS_RIDER_USER = "is_rider_user";
        public static final String IS_RIDER_STATE = "rider_state";
        public static final String KEY_UID = "uid";
        public static final String KEY_EOS_ACCOUNT = "eos_account";

    }


    public static String getError(Context context) {
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getString(ERROR, "");
    }

    public static void saveError(Context context, String error) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(ERROR, error);
        edit.commit();
    }

    public static String getProDuctId(Context context) {
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getString(LocalUser.PRODUCT_ID, "");
    }

    public static void saveProDuctId(Context context, String error) {
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(LocalUser.PRODUCT_ID, error);
        edit.commit();
    }


    public static void saveStringPreference(Context context,String key,String value){
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putString(key, value);
        edit.commit();
    }

    public static void saveBooleanPreference(Context context,String key,boolean value){
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    public static void saveIntPreference(Context context,String key,int value){
        SharedPreferences.Editor edit = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE).edit();
        edit.putInt(key, value);
        edit.commit();
    }


    public static String getStringPreference(Context context,String key){
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getString(key,"");
    }

    public static boolean getBooleanPreference(Context context,String key){
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getBoolean(key,false);
    }


    public static int getIntPreference(Context context,String key){
        SharedPreferences preference = context.getSharedPreferences(SHARED_ACTIVITY_NAME, Context.MODE_PRIVATE);
        return preference.getInt(key,0);
    }




}
